/**
 * @date 2006-7-27 17:05:58
 *
 */
package org.nanhill.moo.config;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;

import org.springframework.util.DefaultPropertiesPersister;
import org.springframework.util.PropertiesPersister;

/**
 * @author alin [xalinx at gmail dot com]
 * 
 */
public class Configuration implements Configurable {
	private static final String MAX_USER_TAG = "max_user_tag";

	private static final String MAX_FAVOR_TAG = "max_favor_tag";

	/**
	 * Max tag num of one user.
	 */
	private int maxUserTag;

	/**
	 * Max tag num of one favor.
	 */
	private int maxFavorTag;

	/**
	 * Config property file.
	 */
	private Properties properties;

	/**
	 * Web application path.
	 */
	private String webappPath = null;

	private Configuration() {
		try {
			init();
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	private void loadProps() throws IOException {
		properties = new Properties();
		PropertiesPersister propertiesPersister = new DefaultPropertiesPersister();
		propertiesPersister.load(properties, new InputStreamReader(this
				.getClass().getResourceAsStream("/conf.properties"), "utf-8"));
	}

	private void init() throws IOException {
		loadProps();
		webappPath = System.getProperty("webapp.root");
		maxUserTag = Integer.parseInt(properties.getProperty(MAX_USER_TAG));
		maxFavorTag = Integer.parseInt(properties.getProperty(MAX_FAVOR_TAG));
	}

	/**
	 * Singleton.
	 */
	private static Configuration conf = new Configuration();

	/**
	 * Get singleton instance.
	 * 
	 * @return
	 */
	public static Configuration getInstance() {
		return conf;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.nanhill.moo.config.Configurable#getUserTagMaxNumber()
	 */
	public int getUserTagMaxNumber() {
		return maxUserTag;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.nanhill.moo.config.Configurable#getFavorTagMaxNumber()
	 */
	public int getFavorTagMaxNumber() {
		return maxFavorTag;
	}

	public String getWebappPath() {
		return webappPath;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.nanhill.moo.config.Configurable#getChildsCategoryCacheTime()
	 */
	public long getCategoryChildsCacheMeantime() {
		return 30000;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.nanhill.moo.config.Configurable#getChildsCategoryCacheTime()
	 */
	public long getCostsCacheMeantime() {
		return 30000;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.nanhill.moo.config.Configurable#getFavorMaxPageSize()
	 */
	public int getFavorMaxPageSize() {
		return 50;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.nanhill.moo.config.Configurable#getFavorMinPageSize()
	 */
	public int getFavorMinPageSize() {
		return 5;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.nanhill.moo.config.Configurable#getUserMaxPageSize()
	 */
	public int getUserMaxPageSize() {
		return 50;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.nanhill.moo.config.Configurable#getUserMinPageSize()
	 */
	public int getUserMinPageSize() {
		return 5;
	}

}
